package filefinder.finders;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * Simple string search in the file implementation of the Finder 
 * @author denist
 *
 */
public class StringFinder implements Finder {

	public void find(File file, String toFind, List list) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String line;
			while ((line = reader.readLine()) != null) {
				if (line.indexOf(toFind) > -1) {
					synchronized (list) {
						list.add(file.getAbsolutePath());
					}
					break;
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("Cannot open the file due to");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Cannot read the file due to");
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					System.out.println("Cannot close the file due to");
					e.printStackTrace();
				}
			}
		}
	}
}